﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WordWonderGame.UI.Man3
{
    public partial class TraLoiUI : UserControl
    {
        List<string> dapan;
        List<string> CauTraLoi;
        public event EventHandler TraloiSaiCauNay;
        public event EventHandler TraloiDungCauNay;
        bool allowTraloi=false;
        bool allowChonDapAn = true;
        public TraLoiUI()
        {
            InitializeComponent();
            dapan = new List<string>();
            CauTraLoi = new List<string>();
        }
        public void BindCauHoi(List<string> cauhoi)
        {
            dapan = cauhoi.GetRange(0,4);
            cauhoi = cauhoi.OrderBy(x => Guid.NewGuid()).ToList();
            chonDA1.Tu = cauhoi[0];
            chonDA2.Tu = cauhoi[1];
            chonDA3.Tu = cauhoi[2];
            chonDA4.Tu = cauhoi[3];
            chonDA5.Tu = cauhoi[4];
            chonDA6.Tu = cauhoi[5];
            CauTraLoi.Clear();
            allowChonDapAn = true;
            chonDA1.AllowChonDapAn = allowChonDapAn;
            chonDA2.AllowChonDapAn = allowChonDapAn;
            chonDA3.AllowChonDapAn = allowChonDapAn;
            chonDA4.AllowChonDapAn = allowChonDapAn;
            chonDA5.AllowChonDapAn = allowChonDapAn;
            chonDA6.AllowChonDapAn = allowChonDapAn;
        }

        private void chonDA1_ClickChonDA(object sender, EventArgs e)
        {
            CauTraLoi.Add((string)sender);
            if (CauTraLoi.Count == 4)
            {
                allowTraloi = true;
                btTraloi.BackgroundImage = imageList1.Images[1];
                btTraloi.ForeColor = System.Drawing.Color.Yellow;
                btTraloi.Enabled = true;
            }
            else
            {
                allowTraloi = false;
                btTraloi.BackgroundImage = imageList1.Images[0];
                this.btTraloi.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
                btTraloi.Enabled = false;
            }
            
        }

        private void chonDA1_ClickHuyDA(object sender, EventArgs e)
        {
            CauTraLoi.Remove((string)sender);
            if (CauTraLoi.Count == 4)
            {
                allowTraloi = true;
                btTraloi.BackgroundImage = imageList1.Images[1]; 
                btTraloi.ForeColor = System.Drawing.Color.Yellow;
                btTraloi.Enabled = true;
            }
            else
            {
                allowTraloi = false;
                btTraloi.BackgroundImage = imageList1.Images[0];
                this.btTraloi.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
                btTraloi.Enabled = false;
            }
        }



        private void btTraloi_Click(object sender, EventArgs e)
        {
            if (allowTraloi == true)
            {
                allowTraloi = false;
                btTraloi.BackgroundImage = imageList1.Images[0];
                this.btTraloi.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
                btTraloi.Enabled = false;

                allowChonDapAn = false;

                chonDA1.AllowChonDapAn = allowChonDapAn;
                chonDA2.AllowChonDapAn = allowChonDapAn;
                chonDA3.AllowChonDapAn = allowChonDapAn;
                chonDA4.AllowChonDapAn = allowChonDapAn;
                chonDA5.AllowChonDapAn = allowChonDapAn;
                chonDA6.AllowChonDapAn = allowChonDapAn;
                foreach (string s in CauTraLoi)
                {
                    if (dapan.IndexOf(s) < 0)
                    {
                        chonDA1.lbTu.ForeColor = System.Drawing.Color.Red;
                        chonDA2.lbTu.ForeColor = System.Drawing.Color.Red;
                        chonDA3.lbTu.ForeColor = System.Drawing.Color.Red;
                        chonDA4.lbTu.ForeColor = System.Drawing.Color.Red;
                        chonDA5.lbTu.ForeColor = System.Drawing.Color.Red;
                        chonDA6.lbTu.ForeColor = System.Drawing.Color.Red;
                        if (dapan.IndexOf(chonDA1.Tu) < 0)
                        {
                            chonDA1.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }
                        if (dapan.IndexOf(chonDA2.Tu) < 0)
                        {
                            chonDA2.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }
                        if (dapan.IndexOf(chonDA3.Tu) < 0)
                        {
                            chonDA3.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }
                        if (dapan.IndexOf(chonDA4.Tu) < 0)
                        {
                            chonDA4.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }
                        if (dapan.IndexOf(chonDA5.Tu) < 0)
                        {
                            chonDA5.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }
                        if (dapan.IndexOf(chonDA6.Tu) < 0)
                        {
                            chonDA6.lbTu.ForeColor = System.Drawing.Color.Blue;
                        }

                        if (TraloiSaiCauNay != null)
                        {
                            TraloiSaiCauNay(null, null);
                        }
                        return;
                    }
                }
                if (TraloiDungCauNay != null)
                {
                    TraloiDungCauNay(null, null);
                }
            }
        }

        public void LoadTraLoi()
        {
            allowTraloi = false;
            btTraloi.BackgroundImage = imageList1.Images[0];
            this.btTraloi.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            btTraloi.Enabled = false;

            allowChonDapAn = false;

            chonDA1.AllowChonDapAn = allowChonDapAn;
            chonDA2.AllowChonDapAn = allowChonDapAn;
            chonDA3.AllowChonDapAn = allowChonDapAn;
            chonDA4.AllowChonDapAn = allowChonDapAn;
            chonDA5.AllowChonDapAn = allowChonDapAn;
            chonDA6.AllowChonDapAn = allowChonDapAn;
        }


        public void HienThiCauSai()
        {
            chonDA1.lbTu.ForeColor = System.Drawing.Color.Red;
            chonDA2.lbTu.ForeColor = System.Drawing.Color.Red;
            chonDA3.lbTu.ForeColor = System.Drawing.Color.Red;
            chonDA4.lbTu.ForeColor = System.Drawing.Color.Red;
            chonDA5.lbTu.ForeColor = System.Drawing.Color.Red;
            chonDA6.lbTu.ForeColor = System.Drawing.Color.Red;
            if (dapan.IndexOf(chonDA1.Tu) < 0)
            {
                chonDA1.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
            if (dapan.IndexOf(chonDA2.Tu) < 0)
            {
                chonDA2.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
            if (dapan.IndexOf(chonDA3.Tu) < 0)
            {
                chonDA3.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
            if (dapan.IndexOf(chonDA4.Tu) < 0)
            {
                chonDA4.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
            if (dapan.IndexOf(chonDA5.Tu) < 0)
            {
                chonDA5.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
            if (dapan.IndexOf(chonDA6.Tu) < 0)
            {
                chonDA6.lbTu.ForeColor = System.Drawing.Color.Blue;
            }
        }
    }
}
